<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.SessionState;
using <xsl:value-of select="Schema/@BusinessNameSpace" />;

namespace <xsl:value-of select="Schema/@NameSpace" />
{
	/// <summary>
	/// <xsl:value-of select="Schema/@TableName" />所有记录的分页列表
	/// </summary>
	public class <xsl:value-of select="Schema/@TableName" />List : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox txtPageSize;
		protected System.Web.UI.WebControls.TextBox txtCurrentPage;
		protected System.Web.UI.WebControls.Button btnGo;
		protected System.Web.UI.WebControls.Label lblRecordCount;
		protected System.Web.UI.WebControls.Label lblPageCount;
		protected System.Web.UI.WebControls.RangeValidator RangeValidator1;
		protected System.Web.UI.WebControls.RangeValidator RangeValidator2;
		protected System.Web.UI.WebControls.ValidationSummary pageValidation;
		protected System.Web.UI.WebControls.DataGrid dgResult;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!IsPostBack)
			{
				// 默认显示第一页
				BindData(0);
			}
		}

		/// <summary>
		/// 查询要绑定在DataGrid的数据。如果要获取其它数据，请修改这个方法
		/// </summary>
		/// <returns>包含查询结果的数据集</returns>
		private DataTable GetData()
		{
<xsl:if test="Schema/@IsStatic='True'">
			return <xsl:value-of select="Schema/@TableName" />Biz.GetAll<xsl:value-of select="Schema/@TableName" />();</xsl:if><xsl:if test="Schema/@IsStatic='False'">
			<xsl:value-of select="Schema/@TableName" />Biz obj<xsl:value-of select="Schema/@TableName" />Biz = new <xsl:value-of select="Schema/@TableName" />Biz();
			return obj<xsl:value-of select="Schema/@TableName" />Biz.GetAll<xsl:value-of select="Schema/@TableName" />();</xsl:if>
		}

		private void dgResult_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
			{
				LinkButton btnDelete = (LinkButton)e.Item.FindControl("btnDelete");
				<xsl:for-each select="Schema/Columns[IS_PK='1']">Label lab<xsl:value-of select="COLUMN_NAME" /> = (Label)e.Item.FindControl("lab<xsl:value-of select="COLUMN_NAME" />");
				</xsl:for-each>
				btnDelete.Attributes.Add("onclick","return confirm('删除数据将不可恢复，是否确定？')");
			}
		}

		private void dgResult_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			<xsl:for-each select="Schema/Columns[IS_PK='1']">
			<xsl:if test="LANGUAGE_TYPE!='System.String'">
			<xsl:value-of select="concat(LANGUAGE_TYPE,' ',COLUMN_NAME)" /> = <xsl:value-of select="LANGUAGE_TYPE" />.Parse(((Label)e.Item.FindControl("lab<xsl:value-of select="COLUMN_NAME" />")).Text);
			</xsl:if>
			<xsl:if test="LANGUAGE_TYPE='System.String'">
			<xsl:value-of select="concat(LANGUAGE_TYPE,' ',COLUMN_NAME)" /> = ((Label)e.Item.FindControl("lab<xsl:value-of select="COLUMN_NAME" />")).Text;
			</xsl:if>
			</xsl:for-each>
			<xsl:value-of select="Schema/@TableName" />Biz.Delete<xsl:value-of select="Schema/@TableName" />(<xsl:for-each select="Schema/Columns[IS_PK='1']"><xsl:value-of select="COLUMN_NAME" /><xsl:if test="position()!=last()">,</xsl:if></xsl:for-each>);
			BindData(dgResult.CurrentPageIndex);
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}

		private void InitializeComponent()
		{    
			this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
			this.dgResult.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgResult_PageIndexChanged);
			this.dgResult.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgResult_SortCommand);
			this.dgResult.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgResult_DeleteCommand);
			this.dgResult.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgResult_ItemDataBound);
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion

		#region 分页和排序代码，请不要使用代码编辑器修改
		/// <summary>
		/// 绑定数据，不改变此方法
		/// </summary>
		/// <param name="intPage">页码</param>
		private void BindData(int intPage)
		{
			DataTable dtQuery = GetData();
			DataView dvQuery = new DataView(dtQuery);
			dgResult.DataSource = dvQuery;

			dvQuery.Sort = (string)ViewState["Sort"];				// 指定排序列
			dgResult.PageSize = Convert.ToInt32(txtPageSize.Text);	// 指定页尺寸

			// 预绑定
			dgResult.CurrentPageIndex = 0;
			dgResult.DataBind();

			if(intPage <xsl:text disable-output-escaping="yes">&lt;</xsl:text> 0)
				intPage = 0;										//指定当前页
			else if(intPage <xsl:text disable-output-escaping="yes">&gt;</xsl:text>= dgResult.PageCount  <xsl:text disable-output-escaping="yes">&amp;&amp;</xsl:text> dgResult.PageCount != 0)
				intPage = dgResult.PageCount-1;

			dgResult.CurrentPageIndex = intPage;					//指定当前页

			lblRecordCount.Text = dtQuery.Rows.Count.ToString();
			dgResult.DataBind();				// 绑定数据
			lblPageCount.Text = dgResult.PageCount.ToString();
			txtCurrentPage.Text = (intPage + 1).ToString();
		}

		private void dgResult_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			BindData(e.NewPageIndex);
		}

		private void dgResult_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			if(e.SortExpression == (string)ViewState["Sort"])
				ViewState["Sort"] = e.SortExpression + " desc";
			else
				ViewState["Sort"] = e.SortExpression;

			BindData(0);
		}

		private void btnGo_Click(object sender, System.EventArgs e)
		{
			BindData(Convert.ToInt32(txtCurrentPage.Text) - 1);
		}
		#endregion
	}
}</xsl:template>
</xsl:stylesheet>